<template>
  <div>
    <div class="upload_container">
      <h3>单一文件上传「FORM-DATA」</h3>

      <div class="upload_box">
        <input
          ref="upload_ipu"
          @change="getFile"
          type="file"
          class="upload_ipu"
        />
        <div class="upload_button_group">
          <button
            class="upload_button select"
            :class="uploadStatus ? 'disabled' : ''"
            @click="selectFile"
          >
            选择文件
          </button>
          <button
            class="upload_button upload"
            :class="uploadStatus ? 'loading' : ''"
            @click="uploadFile"
          >
            上传到服务器
          </button>
        </div>
        <div class="upload_tips" v-if="!showFile">
          只能上传 PNG/JPG/JPEG 格式图片，且大小不能超过2MB
        </div>
        <div class="upload_list" v-if="showFile">
          <span>文件:{{ file.name }}</span>
          <span><em @click="removeFile">移除</em></span>
        </div>
      </div>

      <div class="image_box" v-if="imgUrl">
        <img :src="imgUrl" alt="" />
      </div>
    </div>
  </div>
</template>

<script>
import { uploadSingle } from "../../api/upload";
export default {
  data() {
    return {
      showFile: false,
      file: "",
      imgUrl: "",
      uploadStatus: false,
    };
  },
  mounted() {},
  methods: {
    selectFile() {
      this.$refs["upload_ipu"].click();
    },
    getFile(event) {
      this.file = event.target.files["0"];
      if (this.file) {
        this.showFile = true;
      }
    },
    removeFile() {
      this.file = "";
      this.showFile = false;
    },
    async uploadFile() {
      if (this.file.size > 2 * 1024 * 1024) {
        this.$message.warning("上传的文件不能大于2MB");
        return;
      }
      let type = this.file.type;
      if (!/(PNG|JPEG|JPG|GIF)/i.test(type)) {
        this.$message.warning("上传的文件类型不正确");
        return;
      }
      this.uploadStatus = true;
      let formData = new FormData();
      formData.append("file", this.file);
      formData.append("filename", this.file.name);
      try {
        let res = await uploadSingle(formData);
        this.$message.success("上传文件成功");
        this.imgUrl = res.data.servicePath;
        this.uploadStatus = false;
        return Promise.reject();
      } catch (error) {
        this.$message.error("上传文件失败");
      }
      this.file = "";
      this.showFile = false;
      this.uploadStatus = false;
    },
  },
};
</script>

<style scoped lang='scss'>
.upload_container {
  padding: 40px;
}
.upload_container h3 {
  font-size: 18px;
  font-weight: 700;
}
.upload_box {
  border: 1px dashed #ddd;
  padding: 20px;
  width: 340px;
  min-height: 120px;
}
.upload_button {
  border: none;
  background: antiquewhite;
  color: #fff;
  font-size: 12px;
  text-align: center;
  padding: 0 10px;
  height: 24px;
  line-height: 24px;
  margin-right: 10px;
  cursor: pointer;
  border-radius: 5px;
}
.select {
  background-color: rgb(6, 209, 131);
}
.upload {
  background: rgb(3, 151, 250);
  position: relative;
}
.upload_tips {
  color: #3d3d3d;
  font-size: 12px;
  margin-top: 10px;
}
.upload_list {
  font-size: 12px;
  color: #3d3d3d;
  margin-top: 10px;
}
.upload_list em {
  margin-left: 10px;
  color: red;
  cursor: pointer;
  vertical-align: middle;
  font-style: normal;
}
.upload_ipu {
  display: none;
}
.image_box {
  border: 1px dashed #ddd;
  padding: 20px;
  width: 340px;
  margin: 10px 0;
  text-align: center;
}
.disabled {
  background: #ccc;
}
.loading:after {
  position: absolute;
  content: "loading...";
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  background: #ddd url("../../assets/css/loading.gif") no-repeat 10px center;
  z-index: 999;
}
</style>
